package com.smartfast4j.manager.modules.sys.controller;

import com.google.code.kaptcha.Producer;
import com.smartfast4j.common.Constants;
import com.smartfast4j.common.annotation.LogRecord;
import com.smartfast4j.common.annotation.NotEmpty;
import com.smartfast4j.common.bean.R;
import com.smartfast4j.manager.common.bean.LoginUserInfo;
import com.smartfast4j.manager.common.controller.ManagerBaseController;
import com.smartfast4j.manager.modules.sys.service.SysUserService;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * 登录Controller
 */
@Controller
public class SysLoginController extends ManagerBaseController {

    @Autowired
    private Producer kaptchaProducer;//用于生成验证码

    @Autowired
    private SysUserService sysUserService;

    /**
     * 生成图片验证码
     * @param response
     * @throws IOException
     */
    @RequestMapping("/captcha.jpg")
    public void captcha(HttpServletResponse response) throws IOException {
        response.setHeader("CacheService-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");
        //生成文字验证码
        String text = kaptchaProducer.createText();
        //生成图片验证码
        BufferedImage image = kaptchaProducer.createImage(text);
        logger.debug("图片验证码="+text);
        //将验证码保存到缓存中
        cacheService.set(Constants.VERIFY_CODE, text);
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
        IOUtils.closeQuietly(out);
    }

    /**
     * 登录
     */
    @LogRecord(operation="登录系统",remark = "系统用户进行登录",logType = 1)//使用自定义的LogRecord注解记录操作日志，logType = 1表示这是一个登录日志
    @NotEmpty(value = {"username","password","captcha"},message = {"请输入用户名","请输入密码","请输入验证码"})//使用自定义注解进行非空校验
    @ResponseBody
    @RequestMapping("/sys/login")
    public R login(String username, String password, String captcha){
        //从缓存中获取验证码
        String kaptcha = cacheService.get(Constants.VERIFY_CODE,true);
        //校验验证码
        if(!captcha.equalsIgnoreCase(kaptcha)){
            return R.fail("验证码不正确");
        }
        //用户登录
        return sysUserService.login(username,password);
    }

    /**
     * 退出
     */
    @LogRecord(operation="退出系统",remark = "系统用户退出登录")//使用自定义的LogRecord注解记录操作日志
    @RequestMapping("/sys/logout")
    @ResponseBody
    public R logout(String token) {
       sysUserService.logout(token);
       return R.ok();
    }

    /**
     * 获取登录用户信息
     * @return
     */
    @RequestMapping("/sys/getLoginUserInfo")
    @ResponseBody
    public R getLoginUserInfo(){
        LoginUserInfo loginUser = getCurrLoginUser();
        return R.ok(loginUser);
    }
}
